//
//  CollectViewController.m
//  美食百科
//
//  Created by dllo on 16/2/1.
//  Copyright © 2016年 doll. All rights reserved.
//

#import "CollectViewController.h"
#import "DataBaseHandle.h"
#import "StapleTableViewCell.h"
#import "InforViewController.h"
#import "CollectModel.h"
#define kWidth self.view.frame.size.width
#define kHeight self.view.frame.size.height

@interface CollectViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UITableView *tableView;
@property(nonatomic,retain)NSMutableArray *collectArray;
@property(nonatomic,retain)UILabel *lable;
@end

@implementation CollectViewController
- (void)dealloc
{   [_item release];
    [_collectArray release];
    [_tableView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIBarButtonItem *leftItemFirst = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-zuojiantou-3"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
    UIBarButtonItem *leftItemSecond = [[UIBarButtonItem alloc]initWithTitle:self.item style:UIBarButtonItemStyleDone target:self action:nil];
    self.navigationItem.leftBarButtonItems = @[leftItemFirst,leftItemSecond];
    self.collectArray = [NSMutableArray array];
 self.collectArray = [[DataBaseHandle shareDataBaseHandle]select];
    NSLog(@"%@",self.collectArray);
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight-64) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.bounces = NO;
    [self.view addSubview:self.tableView];
    [_tableView release];
    [self.tableView registerClass:[StapleTableViewCell class] forCellReuseIdentifier:@"collect"];
//    if (self.collectArray.count == 0) {
//       self.lable = [[UILabel alloc]initWithFrame:CGRectMake(0, kHeight/3, kWidth, 40)];
//        self.lable.font = [UIFont systemFontOfSize:15];
//        self.lable.textColor = [UIColor grayColor];
//        self.lable.textAlignment = NSTextAlignmentCenter;
//        self.lable.text = @"还没有收藏~";
//        [self.view addSubview:self.lable];
//        [self.view bringSubviewToFront:self.lable];
//        [_lable release];
//    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.collectArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    StapleTableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"collect"];
    cell.collect = self.collectArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}



//跳转详情界面
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    InforViewController *infor = [[InforViewController alloc]init];
    infor.number = 3;
    CollectModel *collect = self.collectArray[indexPath.row];
    
    infor.name = collect.code;
    [self.navigationController pushViewController:infor animated:YES];
    
    [infor release];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kHeight/7;
}
-(void)backAction:(UIBarButtonItem *)item{
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
    
}
-(void)viewDidDisappear:(BOOL)animated
{
    [self.lable removeFromSuperview];
}
-(void)viewDidAppear:(BOOL)animated
{
    self.collectArray = [[DataBaseHandle shareDataBaseHandle]select];
    [self.tableView reloadData];
    if (self.collectArray.count == 0) {
        self.lable = [[UILabel alloc]initWithFrame:CGRectMake(0, kHeight/4, kWidth, 40)];
        self.lable.textColor = [UIColor grayColor];
        self.lable.font = [UIFont systemFontOfSize:15];

        self.lable.textAlignment = NSTextAlignmentCenter;
        self.lable.text = @"还没有收藏~";
        [self.view addSubview:self.lable];
        [self.view bringSubviewToFront:self.lable];
        [_lable release];
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
